Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Web Coding › .htaccess - what am I doing wrong?
New Posts  All Forums:Forum Nav:

.htaccess - what am I doing wrong?

post #1 of 5
Thread Starter 
Hi everybody, I discovered recently that it was possible to shorten website URLs with the .htaccess file.

I want it configured so it would just hide the file extensions (php) for the links.
(Maybe there are better options? Any suggestions are much appreciated!)
Code:
RewriteEngine On

# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http:/www.example.com/$1 [R=301,L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://www.example.com/$1 [R=301,L]

# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]


This is what I am using currently. The webpage has a lot of pages (over 50) and there is a slight problem with this .htaccess code.

It works very well with the root files, but if the files are in different folders,
eg: "/abc/test/qwe.php", then it will say that the file does not exist on the server.
The URL will lead to example.com/abc/test/qwe and it just doesn't know that the file is there.

What could be wrong?

I hope that there is someone here who can understand the code. smile.gif
Edited by Karlz3r - 12/2/12 at 4:22pm
Nerfed
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600K 4~5.0 GHz ASUS P8Z68-V/GEN3 Gigabyte 7770 OC 1GB G.Skill Sniper 2x4GB DDR3-1600 1.25V 
Hard DriveHard DriveHard DriveCooling
Corsair M4 128GB Seagate 7200.14 3TB (RAID-1) Toshiba DT01ACA300 3TB (RAID-1) Silver Arrow 
OSMonitorPowerCase
W7 64 24" Dell E248WFP Antec EarthWatts Green 380W (80+ Bronze) Enermax iVektor 
Mouse
Logitech G9 
  hide details  
Reply
Nerfed
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600K 4~5.0 GHz ASUS P8Z68-V/GEN3 Gigabyte 7770 OC 1GB G.Skill Sniper 2x4GB DDR3-1600 1.25V 
Hard DriveHard DriveHard DriveCooling
Corsair M4 128GB Seagate 7200.14 3TB (RAID-1) Toshiba DT01ACA300 3TB (RAID-1) Silver Arrow 
OSMonitorPowerCase
W7 64 24" Dell E248WFP Antec EarthWatts Green 380W (80+ Bronze) Enermax iVektor 
Mouse
Logitech G9 
  hide details  
Reply
post #2 of 5
Thread Starter 
Please help me! redface.gif
Nerfed
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600K 4~5.0 GHz ASUS P8Z68-V/GEN3 Gigabyte 7770 OC 1GB G.Skill Sniper 2x4GB DDR3-1600 1.25V 
Hard DriveHard DriveHard DriveCooling
Corsair M4 128GB Seagate 7200.14 3TB (RAID-1) Toshiba DT01ACA300 3TB (RAID-1) Silver Arrow 
OSMonitorPowerCase
W7 64 24" Dell E248WFP Antec EarthWatts Green 380W (80+ Bronze) Enermax iVektor 
Mouse
Logitech G9 
  hide details  
Reply
Nerfed
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600K 4~5.0 GHz ASUS P8Z68-V/GEN3 Gigabyte 7770 OC 1GB G.Skill Sniper 2x4GB DDR3-1600 1.25V 
Hard DriveHard DriveHard DriveCooling
Corsair M4 128GB Seagate 7200.14 3TB (RAID-1) Toshiba DT01ACA300 3TB (RAID-1) Silver Arrow 
OSMonitorPowerCase
W7 64 24" Dell E248WFP Antec EarthWatts Green 380W (80+ Bronze) Enermax iVektor 
Mouse
Logitech G9 
  hide details  
Reply
post #3 of 5
http://httpd.apache.org/docs/current/mod/mod_rewrite.html#logging

I've always had issues with .htaccess, especially when I'm using shared hosting.
Run a local server, and do some test runs and check the log.
Ferberite
(14 items)
 
  
CPUMotherboardGraphicsRAM
Intel(R) Core(TM) i5-2450M CPU @ 2.50GHz Lenovo Thinkpad Edge E520 AMD Radeon HD 6630M 6GB DDR3 @ 1333MHz  
Hard DriveHard DriveOSOS
Samsung 850EVO HITACHI HTS727550A9E364 7.2krpm Debian 7.0 Win7 
OSMonitorMonitorKeyboard
Win8 15.6 Zoll 16:9, 1366x768 Pixel, AUO23EC, spiege... 24" Dell U2412M, 1920x1200 Integrated + External 
PowerCase
20V, 4.5A Lenovo Thinkpad Edge 
  hide details  
Reply
Ferberite
(14 items)
 
  
CPUMotherboardGraphicsRAM
Intel(R) Core(TM) i5-2450M CPU @ 2.50GHz Lenovo Thinkpad Edge E520 AMD Radeon HD 6630M 6GB DDR3 @ 1333MHz  
Hard DriveHard DriveOSOS
Samsung 850EVO HITACHI HTS727550A9E364 7.2krpm Debian 7.0 Win7 
OSMonitorMonitorKeyboard
Win8 15.6 Zoll 16:9, 1366x768 Pixel, AUO23EC, spiege... 24" Dell U2412M, 1920x1200 Integrated + External 
PowerCase
20V, 4.5A Lenovo Thinkpad Edge 
  hide details  
Reply
post #4 of 5
Give this a go:
Code:
RewriteEngine On

# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/folder/$1 [R=301,L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/folder/$1 [R=301,L]

# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]

As for how it works, read up on regular expressions. [ Clicky ]
     
CPUMotherboardGraphicsGraphics
i7 2600K ASUS P8Z68 Deluxe GTX 280 - Died EVGA GeForce GTX 760 Super Clocked 
RAMHard DriveOSMonitor
2x 4GB G.SKILL PC-16000 2x Corsair Force GT 120GB (RAID-0), 3TB Hitachi DS Windows 8.1 64-bit 2x Dell U2410 
MonitorKeyboardCaseMouse
1x Dell U2415 Logitec Wireless Illuminated Keyboard (K800) Fractal Design Define R3: Silver Arrow Logitec Performance Mouse MX 
CPUMotherboardGraphicsRAM
Intel Core i7 6700K Z170MX-Gaming 5 EVGA GeForce GTX 760 Super Clocked 2x G.SKILL TridentZ Series 16GB DDR4 3200 (Blac... 
Hard DriveHard DriveCoolingOS
Samsung SSD 850 EVO 500GB 3TB Hitachi HDS723030ALA640 Phanteks Slim Low-Profile 120mm PWM CPU Cooler ... Windows 10 Professional (64-bit) 
MonitorMonitorMonitorKeyboard
2x Dell U2410 1x Dell U2415 Insignia 46" NS-46L780A12 Logitec Wireless Illuminated Keyboard (K800) 
PowerCaseMouseMouse Pad
EVGA SuperNOVA 850 G2 Corsair Carbide Series Air 240 (White) Logitec Performance Mouse MX Vipamz Extended XXXL Gaming Mouse Pad (White) 
  hide details  
Reply
     
CPUMotherboardGraphicsGraphics
i7 2600K ASUS P8Z68 Deluxe GTX 280 - Died EVGA GeForce GTX 760 Super Clocked 
RAMHard DriveOSMonitor
2x 4GB G.SKILL PC-16000 2x Corsair Force GT 120GB (RAID-0), 3TB Hitachi DS Windows 8.1 64-bit 2x Dell U2410 
MonitorKeyboardCaseMouse
1x Dell U2415 Logitec Wireless Illuminated Keyboard (K800) Fractal Design Define R3: Silver Arrow Logitec Performance Mouse MX 
CPUMotherboardGraphicsRAM
Intel Core i7 6700K Z170MX-Gaming 5 EVGA GeForce GTX 760 Super Clocked 2x G.SKILL TridentZ Series 16GB DDR4 3200 (Blac... 
Hard DriveHard DriveCoolingOS
Samsung SSD 850 EVO 500GB 3TB Hitachi HDS723030ALA640 Phanteks Slim Low-Profile 120mm PWM CPU Cooler ... Windows 10 Professional (64-bit) 
MonitorMonitorMonitorKeyboard
2x Dell U2410 1x Dell U2415 Insignia 46" NS-46L780A12 Logitec Wireless Illuminated Keyboard (K800) 
PowerCaseMouseMouse Pad
EVGA SuperNOVA 850 G2 Corsair Carbide Series Air 240 (White) Logitec Performance Mouse MX Vipamz Extended XXXL Gaming Mouse Pad (White) 
  hide details  
Reply
post #5 of 5
You don't want to be using redirects as that will literally redirect the user. You want something like this instead:
Code:
RewriteEngine On

# if URI = /pretty/url
RewriteCond %{REQUEST_URI} ^/pretty/url/.*$
# then redirect to /mypage.php with the query strings: dir1 = pretty, dir2 = url
RewriteRule ^/(pretty)/(url)/.*$ /mypage.php?dir1=$1&dir2=$2 [L,QSA]
(obviously you'd need to adapt that for your own use)
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Web Coding
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Web Coding › .htaccess - what am I doing wrong?