1

I have some lines in .htaccess, I'm trying to flatten the URL, but it seems I'm missing something. The URL gets redirected but most of the resources are "NOT FOUND" when the URL is redirected.

Here's the code.

Options -Indexes +FollowSymLinks
RewriteEngine On
###########music#######
RewriteCond %{THE_REQUEST} \ /+profile\.php\?user=([^&]+)&title=([^&]+)&uid=([^&\ ]+)
RewriteRule ^ /%1/%2/%3? [L,R=301]
RewriteCond %{THE_REQUEST} \ /+profile\.php\?user=([^&]+)&title=([^&\ ]+)
RewriteRule ^ /%1/%2? [L,R=301]
#################internally redirect////////////
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ profile.php?user=$1&title=$2&uid=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ profile.php?user=$1&title=$2 [L,QSA]

What am I doing wrong?

Relm
  • 7,923
  • 18
  • 66
  • 113
  • 1
    All lines are commented. What URLs are not working for you? – anubhava Dec 03 '14 at 06:24
  • See this question to enable rewrite trace, which may help you find what is wrong: http://stackoverflow.com/questions/7738170/how-to-debug-htaccess-rewrite-script – Sheepy Dec 03 '14 at 06:28
  • @anubhava, my mistake. Please look at it again. – Relm Dec 03 '14 at 06:42
  • What URL is giving you 404 and where is this htaccess located? – anubhava Dec 03 '14 at 06:44
  • @anubhava The HTACCESS is located inside the "public_html", where the index file is, which is the same directory where profile.php is located. www.site.com/profile.php?user=username&title=engineer&uid=45 redirects to www.site.com/username/engineer/45 and gives errors in the console. its a long list of files and images that can't be found. – Relm Dec 03 '14 at 06:55
  • Is `www.site.com/username/engineer/45` giving 404 OR your css/js/images giving 404? – anubhava Dec 03 '14 at 06:59
  • @anubhava CSS/JS/Images are giving 404 – Relm Dec 03 '14 at 07:01

1 Answers1

1

Looks like you've hit the most common problem people face when switching to pretty URL schemes. Solution is also simple, just use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /.

You can try adding this in your page's HTML header: <base href="/" /> so that every relative URL is resolved from that URL and not the current URL.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • You saved the day. I added the . That solved it. – Relm Dec 03 '14 at 07:09
  • I don't know if I should create a separate question or you can answer here. How do I preserve the the "profile" directory like on the url. Like: www.site.com/profile/username/engineer/48? – Relm Dec 03 '14 at 07:41
  • Try last rule as: `RewriteRule ^profile/([^/]+)/([^/]+)/([^/]+)$ profile.php?user=$1&title=$2&uid=$3 [L,QSA]` – anubhava Dec 03 '14 at 08:55
  • ok that does it internally, also added RewriteRule ^ /profile/%1/%2/%3? [L,R=301] – Relm Dec 03 '14 at 09:18